#include "main.h"
//#include <gdk/gdkkeysyms.h>
#include <iostream>
#include "stdlib.h"
using namespace std;

//main+++++++++++++++++++++++++++++++++++++++++++
int eqn1(int p, int pm, float gr, float dr){
    return ((rand() % ((int)(pm - p * gr)))) - ((rand() % ((int)(p - pm * dr))));
}
int eqn2(int p, int pm, float gr, float dr){
    int birth = p * (gr * ((float)(pm - p) / pm));
    cout << "GRate: " << gr * ((float)(pm - p)/pm) << endl;
    return birth;
}
int eqn3(int p, int pm, float gr, float dr){
    if(pm > p)
        return (gr * (2.0/3.0) * sqrt((pm - p) * (pm - p))) + ((rand() % ((int)(p * gr))));
    return (gr * (2.0/3.0) * -1 * sqrt((p - pm) * (p - pm))) - ((rand() % ((int)(p * dr))));
}
int main(int argc, char* argv[]){
    noguimap::controll();
}
/*
//===============================================
//mainwin++++++++++++++++++++++++++++++++++++++++
//constructors---------------
mainWin::mainWin(){
    #if dbgMsg
    cout << "main window created" << endl;
    #endif
    set_title("Stalemate -- ");
    set_resizable(true);
    resize(1000,500);
    add(drawme);
    drawme.set_visible(true);
}
mainWin::~mainWin(){
}
bool mainWin::on_key_press_event(GdkEventKey* key){
    #if dbgMsg
    cout << "main window got a keypress " << endl;
    #endif
    return drawme.keypress(key);
}
//===============================================
//drawingWin+++++++++++++++++++++++++++++++++++++
//constructors---------------
drawingWin::drawingWin(){
    #if dbgMsg
    cout << "drawing window created" << endl;
    #endif
    set_can_focus();
    offset[0] = 0;
    offset[1] = 0;
    cur[0] = 1;
    cur[1] = 1;
}
drawingWin::~drawingWin(){

}
//controll-------------------
bool drawingWin::on_expose_event(GdkEventExpose* event){
    #if dbgMsg
    cout << "drawing window exposed" << endl;
    #endif
    draw();
    return true;
}
bool drawingWin::on_key_press_event(GdkEventKey* key){
    #if dbgMsg
    cout << "drawing window got a keypress" << endl;
    #endif
    return keypress(key);
}
void drawingWin::draw(){
    #if dbgMsg
    cout << "-drawing-" << endl;
    #endif
    Glib::RefPtr<Gdk::Window> win = get_window();
    if(win == NULL){
        #if dbgMsg
        cout << "fail: -bad window-" << endl;
        #endif
        return;
    }
    Cairo::RefPtr<Cairo::Context> cr = win->create_cairo_context();
    cr->save();
    cr->set_source_rgb(0,0,0);
    cr->rectangle(0, 0, get_width(), get_height());
    cr->fill();
    cr->set_source_rgb(0,1,0);
    ///vertl
    for(int i = 0; i < get_width(); i += tWid){
        cr->move_to(i, 0);
        cr->line_to(i, get_height());
        cr->stroke();
    }
    //horizl
    cr->set_source_rgb(1,0,0);
    for(int i = 0; i < get_height(); i += tHgt){
        cr->move_to(0, i);
        cr->line_to(get_width(), i);
        cr->stroke();
    }
    //icons
    for(int i = offset[0]; i < offset[0] + (get_width() * tWid); i++){
        for(int j = offset[1]; i < offset[1] + (get_height() * tHgt); i++){
            icons.draw(cr, i, j);
        }
    }
    //draw cursor
    cr->set_source_rgb(1,1,1);
    //tlc
    cr->move_to((cur[0] - offset[0])* tWid, (cur[1] - offset[1]) * tHgt);
    cr->line_to(((cur[0] - offset[0]) * tWid) + (tWid / 4), ( (cur[1] - offset[1]) * tHgt));
    cr->move_to((cur[0] - offset[0]) * tWid, (cur[1] - offset[1]) * tHgt);
    cr->line_to(((cur[0] - offset[0]) * tWid), ( (cur[1] - offset[1]) * tHgt) + (tHgt / 4));
    //brc
    cr->move_to((cur[0] - offset[0]) * tWid + tWid,   (cur[1] - offset[1]) * tHgt + tHgt);
    cr->line_to(((cur[0] - offset[0]) * tWid + tWid) - (tWid / 4), ( (cur[1] - offset[1]) * tHgt + tHgt));
    cr->move_to((cur[0] - offset[0]) * tWid + tWid,   (cur[1] - offset[1]) * tHgt + tHgt);
    cr->line_to(((cur[0] - offset[0]) * tWid + tWid), ( (cur[1] - offset[1]) * tHgt + tHgt) - (tHgt / 4));
    //stroke
    cr->stroke();
    //finish
    cr->restore();
}
void drawingWin::scroll(){
    int tedgeDist = cur[1] - offset[1];
    int bedgeDist = offset[1] + (get_height() / tHgt) - cur[1];
    int ledgeDist = cur[0] - offset[0];
    int redgeDist = offset[0] + (get_width() / tWid) - cur[0];

    #if dbgMsg
    cout << "scroll------------------------------" << endl;
    cout << "tedge Distance: " << tedgeDist << endl;
    cout << "bedge Distance: " << bedgeDist << endl;
    cout << "ledge Distance: " << ledgeDist << endl;
    cout << "redge Distance: " << redgeDist << endl;
    cout << "====================================="  << endl;
    #endif
    if(tedgeDist < scrollToleranceVert){
        #if dbgMsg
        cout << "scroll up: " << tedgeDist - scrollToleranceVert << endl;
        #endif
        scroll(d_up, scrollToleranceVert - tedgeDist);
    }
    if(bedgeDist < scrollToleranceVert){
        #if dbgMsg
        cout << "scroll down: " << bedgeDist - scrollToleranceVert << endl;
        #endif
        scroll(d_dn, scrollToleranceVert - bedgeDist);
    }
    if(redgeDist < scrollToleranceWid){
        #if dbgMsg
        cout << "scroll right: " << ledgeDist - scrollToleranceVert << endl;
        #endif
        scroll(d_rgt, scrollToleranceVert - redgeDist);
    }
    if(ledgeDist < scrollToleranceWid){
        #if dbgMsg
        cout << "scroll left: " <<  redgeDist - scrollToleranceVert << endl;
        #endif
        scroll(d_lft, scrollToleranceVert - ledgeDist);
    }
}
void drawingWin::scroll(direction dir, int dist){
    switch(dir){
        case d_up:
            if((offset[1] - dist) >= 0){
                offset[1] -= dist;
            }
            if(offset[1] - dist < 0){
                offset[1] = 0;
            }
            break;
        case d_dn:
            if((offset[1] + dist) <= get_height()){
                offset[1] += dist;
            }
            if(offset[1] + dist > get_height()){
                offset[1] = get_height();
            }
            break;
        case d_lft:
            if((offset[0] - dist) >= 0){
                offset[0] -= dist;
            }
            if(offset[0] - dist < 0){
                offset[0] = 0;
            }
            break;
        case d_rgt:
            if((offset[0] + dist) <= get_width()){
                offset[0] += dist;
            }
            if(offset[0] + dist > get_width()){
                offset[0] = get_width();
            }
            break;
        default:
            break;
    }
}
bool drawingWin::keypress(GdkEventKey* key){
    #if dbgMsg
    cout << "got a key-press: " << key->keyval;
    #endif
    int hgt = get_height();
    int wid = get_width();

    switch(key->keyval){
        case GDK_Up:
            if(cur[1] > 0){
                cur[1]--;
            }
            break;
        case GDK_Down:
            if(cur[1] < hgt){
                cur[1]++;
            }
            break;
        case GDK_Left:
            if(cur[0] > 0){
                cur[0]--;
            }
            break;
        case GDK_Right:
            if(cur[0] < wid){
                cur[0]++;
            }
            break;
        default:
            return false;
    }
    scroll();
    draw();
    return true;
}
//===============================================
//icons++++++++++++++++++++++++++++++++++++++++++
icon::icon(){

}
icon::~icon(){

}
void icon::draw(Cairo::RefPtr<Cairo::Context> cr, int x, int y){
    cr->save();
    cr->set_source_rgb(1,.5,.5);
    cr->move_to(x * tWid, y * tHgt);
    cr->line_to(x * tWid + tWid, y * tHgt + tHgt);
    cr->stroke();
    cr->restore();
}
void iconone::draw(Cairo::RefPtr<Cairo::Context> cr, int x, int y){
    cr->save();
    cr->set_source_rgb(.5,1,.5);
    cr->move_to(x * tWid, y * tHgt);
    cr->line_to(x * tWid + tWid, y * tHgt + tHgt);
    cr->stroke();
    cr->restore();
}
void icontwo::draw(Cairo::RefPtr<Cairo::Context> cr, int x, int y){
    cr->save();
    cr->set_source_rgb(.5, .5, 1);
    cr->move_to(x * tWid, y * tHgt);
    cr->line_to(x * tWid + tWid, y * tHgt + tHgt);
    cr->stroke();
    cr->restore();
}
//===============================================
//icon mapping+++++++++++++++++++++++++++++++++++
iconOverlay::iconOverlay(){
    for(int i = 0; i < 30; i++){
        for(int j = 0; j < 30; j++){
            icn[i][j] = new icon();
        }
    }

    for(int i = 30; i < 50; i++){
        for(int j = 0; j < 30; j++){
            icn[i][j] = new iconone();
        }
    }


    for(int i = 0; i < 50; i++){
        for(int j = 30; j < 50; j++){
            icn[i][j] = new icontwo();
        }
    }
}
iconOverlay::~iconOverlay(){
    for(int i = 0; i < 50; i++){
        for(int j = 0; j < 50; j++){
            delete icn[i][j];
        }
    }
}
void iconOverlay::draw(Cairo::RefPtr<Cairo::Context> cr, int x, int y){
    if(x < 50 && y < 50)
    icn[x][y]->draw(cr, x, y);
}
//===============================================
 * */